package com.bianmaba.activiti.service.authorization.resource;

import com.bianmaba.activiti.beans.entity.authorization.Resource;
import com.bianmaba.spring.security.details.ResourceDetailsServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomResourceDetailsService extends ResourceDetailsServiceImpl {
    private static final Logger LOG = LoggerFactory.getLogger(CustomResourceDetailsService.class);
    @Autowired
    private IResourceService resourceService;

    public CustomResourceDetailsService() {
        LOG.info("加载自定义ResourceDetailsService：" + CustomResourceDetailsService.class.getName());
    }

    @Override
    public List<String> getAllResources() {
        List<String> result = new ArrayList<>(0);
        List<Resource> resources = resourceService.findAll();
        for (Resource resource : resources) {
            Map<String, String> data = new HashMap<>(0);
            String path = resource.getUrl();
            result.add(path);
        }
        return result;
    }

    @Override
    public List<String> findAuthorities(String resource) {
        String sql = "select role.name as name from  system_role role  join system_resource_ref_role rr on role.id=rr.role_id  join system_resource resource on rr.resource_id=resource.id\n" +
                "where resource.url=?";
        List<String> roles = this.resourceService.findRoleNames(resource);
        return roles;
    }
}
